<?php
namespace app\api\controller;

use app\service\UserService;
use think\Db;

class Author extends Common
{
    /**
     * 构造方法
     */
    public function __construct()
    {
        parent::__construct();
    }
    
    /**
     * 作者列表
     */
    public function Index()
    {
       

        // 参数
        $params = $this->data_request;      
   
        // 分页
        $number = 18;
        $page = max(1, isset($this->data_post['page']) ? intval($this->data_post['page']) : 1);

        // 条件
         $where =[];
        if(!empty($params['keywords']))
        {
            $where = ['name', 'like', '%'.$params['keywords'] . '%'];
        }

       

        // 获取总数
        $total = Db::name('author')->where($where)->count();
        $page_total = ceil($total/$number);
        $start = intval(($page-1)*$number);

       
        $data = Db::name('author')->where($where)->limit($start,$number)->order("head_img desc,follow_num desc")->select();

        // 返回数据
        $result = [
            'total'         =>  $total,
            'page_total'    =>  $page_total,
            'data'          =>  $data,
        ];
        return DataReturn('success', 0, $result);
    }

    //作者详情
    public function Detail(){
    	// 参数
        $params = $this->data_request; 

        if(empty($params['id'])){
        	 return DataReturn('参数有误', -1);
        }  

        $data=Db::name("author")->find($params['id']);

        return DataReturn('success', 0, $data);
    }

    //作者书籍
    public function authorgoods(){
    	// 参数
        $params = $this->data_request; 

        if(empty($params['id'])){
        	 return DataReturn('参数有误', -1);
        }  


        // 分页
        $number = 10;
        $page = max(1, isset($params['page']) ? intval($params['page']) : 1);

          // 获取总数
        $total = Db::name('goods')->where(['author'=>$params['id']])->group('isbn')->count();
        $page_total = ceil($total/$number);
        $start = intval(($page-1)*$number);


        $data=Db::name("goods")->where(['author'=>$params['id']])->field("id,title,author,inventory,quality,original_price,price,images,is_delete_time")->limit($start,$number)->group('isbn')->select();

          // 返回数据
        $result = [
            'total'         =>  $total,
            'page_total'    =>  $page_total,
            'data'          =>  $data,
        ];
        return DataReturn('success', 0, $result);
    }

    //关注作者
    public function followauthor(){
    	$params = $this->data_post;
        $params['user'] = $this->user;


            // 请求参数
        $p = [
            [
                'checked_type' => 'empty',
                'key_name' => 'author_id',
                'error_msg' => '作者id有误',
            ],
            [
                'checked_type' => 'empty',
                'key_name' => 'user',
                'error_msg' => '用户信息有误',
            ],
        ];

        $ret = ParamsChecked($params, $p);

        if ($ret !== true) {
            return DataReturn($ret, -1);
        }

        // 查询用户状态是否正常
        $ret = UserService::UserStatusCheck('id', $params['user']['id']);
        if ($ret['code'] != 0) {
            return $ret;
        }


        // 开始操作
        $data = ['author_id'=>intval($params['author_id']), 'user_id'=>$params['user']['id']];

        $temp = Db::name('AuthorFavor')->where($data)->find();
        if (empty($temp)) {
            // 添加收藏
            $data['add_time'] = time();
            if (Db::name('AuthorFavor')->insertGetId($data) > 0) {
            	Db::name("Author")->where(['id'=>$params['author_id']])->setInc("follow_num");
                return DataReturn('关注成功', 0, [
                    'text' => '已关注',
                    'status' => 1,
                    'count' => Db::name('AuthorFavor')->where(['author_id' => $data['author_id']])->count(),
                ]);
            } else {
                return DataReturn('关注失败');
            }
        } else {
          

            // 删除收藏
            if (Db::name('AuthorFavor')->where($data)->delete() > 0) {
            	Db::name("Author")->where(['id'=>$params['author_id']])->setDec("follow_num");
                return DataReturn('取消成功', 0, [
                    'text' => '关注',
                    'status' => 0,
                    'count' =>Db::name('AuthorFavor')->where(['author_id' => $data['author_id']])->count(),
                ]);
            } else {
                return DataReturn('取消失败');
            }
        }
        
    }
    
}
?>